package com.lw.chat.view.chat;

import com.lw.chat.model.ChatModel;
import com.lw.chat.model.Message;
import de.saxsys.mvvmfx.FluentViewLoader;
import de.saxsys.mvvmfx.ViewModel;
import de.saxsys.mvvmfx.ViewTuple;
import de.saxsys.mvvmfx.utils.commands.Action;
import de.saxsys.mvvmfx.utils.commands.Command;
import de.saxsys.mvvmfx.utils.commands.DelegateCommand;
import de.saxsys.mvvmfx.utils.mapping.ModelWrapper;
import javafx.application.Platform;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.util.Random;

public class ChatViewModel implements ViewModel {

    private ModelWrapper<ChatModel> wrapper = new ModelWrapper<>();



    private Command chatCommand;
    private Command msgCommand;

    private ObservableList<ChatModel> chatDataList = FXCollections.observableArrayList();
    private ObservableList messageDataList = FXCollections.observableArrayList();


    public ChatViewModel() {

        chatCommand = new DelegateCommand(() -> new Action() {
            @Override
            protected void action() throws Exception {
                loadingChat();
            }
        }, true); //Async
        msgCommand = new DelegateCommand(() -> new Action() {
            @Override
            protected void action() throws Exception {
                Platform.runLater(() -> updateMessageData());
            }
        }, true); //Async

        chatCommand.execute();
    }


    public Command getChatCommand() {
        return chatCommand;
    }

    public ObservableList<ChatModel> getChatDataList() {
        return chatDataList;
    }

    public ObservableList getMessageDataList() {
        return messageDataList;
    }

    /**
     * 加载聊天
     */
    private void loadingChat() {

        for (int i = 0; i < 100; i++) {
            chatDataList.add(new ChatModel("小码农" + i, "/images/icon-test-2.png", "15:30", "明天出去玩？"));
        }

    }

    public void setChatModel(ChatModel chatModel) {
        wrapper.set(chatModel);
        wrapper.reload();
        updateMessageData();
    }

    public ChatModel getChatModel() {
       return wrapper.get();
    }
    
    public StringProperty nameProperty(){
        return wrapper.field("name", ChatModel::getName, ChatModel::setName, "");
    }

    private void updateMessageData() {
        messageDataList.clear();
        ViewTuple<MessageBoxView, MessageBoxViewModel> viewTuple = FluentViewLoader.fxmlView(MessageBoxView.class).load();
        viewTuple.getViewModel().setMessage(new Message(true,getChatModel().getName(), getChatModel().getAvatarUri(), null, "明天有出去玩的吗？"));
        int random = new Random().nextInt(3);
        messageDataList.addAll(viewTuple.getView());
        for (int i = 0; i < random; i++) {
            ViewTuple<MessageBoxView, MessageBoxViewModel> msgTuple = FluentViewLoader.fxmlView(MessageBoxView.class).load();
            msgTuple.getViewModel().setMessage(new Message(true,getChatModel().getName() + i, "/images/icon-test-2.png", i % 2 > 0 ? null : "/images/123.png", "我去+" + i));

            messageDataList.addAll(msgTuple.getView());
        }
        ViewTuple<MessageBoxMeView, MessageBoxViewMeModel> meTuple = FluentViewLoader.fxmlView(MessageBoxMeView.class).load();
        meTuple.getViewModel().setMessage(new Message(false, "我", "/images/icon-test.png", "/images/123.png", "我也去！"));
        messageDataList.addAll(meTuple.getView());
    }
}
